//
// printoutput.h
// libprotea
// Tools Library for the Protea Project / Server-Side Reporting
// Copyright (C) 2003, 2004, 2005 eXegeSys, Inc.
// Copyright (C) 2008 Bruce A. James
//
// This program is free software; you can redistribute it and/or modify it 
// under the terms of the GNU General Public License as published by the 
// Free Software Foundation; either version 2 of the License, or (at your 
// option) any later version.
//
// This program is distributed in the hope that it will be useful, but 
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
// for more details.
// 
// You should have received a copy of the GNU General Public License along 
// with this program; if not, write to the Free Software Foundation, Inc., 
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// 
// Please send correspondence to:
// 
// theproteaproject@gmail.com
//

#ifndef __PROTEA_PRINTOUTPUT_H__
#define __PROTEA_PRINTOUTPUT_H__

// Forward declarations
namespace XERCES_CPP_NAMESPACE {
	class XercesDOMParser;
	class DOMDocument;
	class DOMNode;
};
class XCBuffer;
class XCTransport;
struct OutputTag;

struct ReportLink {
	XERCES_CPP_NAMESPACE::DOMNode* node;
	int id;
	ReportLink* next;
};

/// Report generation class
class __EXPORT XCPrintOutput : public XCBase {
public:
	/// Constructor
	XCPrintOutput( XCAppInterface* owner );

	/// Destructor
	~XCPrintOutput();

	static void Initialize();
	static void Uninitialize();
	static ReportLink* links;

	/**
		Initialize the report generation
		/param Output report id

		Prepares the report generator.  If the user has not defined any actions for the
		report, then the function will return false.
	*/
	bool Start( int id );

	/**
		Finish the report generation
		
		1) Closes any open tags
		2) Calls the user-defined report action
		3) Cleans up the temporary file
	*/
	void Finish();

	/// Starts a new tag, using the supplied string
	void StartTag( const char* tag );

	/// Starts a new tag, by using the supplied message number
	void StartTag( const int value );

	/// Ends the last opened tag
	void EndTag();

	/// Writes all field values in a buffer to the report file
	void OutputBuffer( const XCBuffer* buffer );

	/// Writes the value of a particular field to the report file
	void OutputField( const XCField* field );

protected:
	enum output_style {
		PDF_OUTPUT,
		PS_OUTPUT
	};

	void HandleStyleSheet( const char* style, const char* filename );
	void CreatePrintFile( output_style style, XERCES_CPP_NAMESPACE::DOMNode* node, bool print );

	char* filename;

	ReportLink* current_report;		// Pointer to current report (no report if null)
	OutputTag* TagList;				// Stores open tags, so that tag name not required for EndTag
	XCAppInterface* app;			// Pointer to application interface
	XERCES_CPP_NAMESPACE::XercesDOMParser* parser;	// XML output information
	XERCES_CPP_NAMESPACE::DOMDocument * DataDOC;	// Data document
	XERCES_CPP_NAMESPACE::DOMNode* current_node;	// Current node in the DOM
};

#endif
